let friendDao = require('../dao/friendDao');
let redis = require('../dao/redis');
let cnst = require('../const');
let userDao = require('../dao/userDao');
let redisConst = require('../const/redisConst');

const { BusiError, SysError } = require('../exception/exception');

class FriendService {
    constructor() {

    }

    async queryFriends(uid, page) {
        let ret = await friendDao.getFriendsByPage(uid, page);
        return ret;
    }

    async isFriend(uid, friendUid) {
        if (uid === friendUid) {
            return true;
        }

        let key = this.getKey(uid, friendUid);
        let cacheKey = redisConst.USER_FRIEND_FLAG + ":" + key;
        let bFriend = await redis.get(cacheKey);
        if (bFriend === '1') {
            return true;
        }

        let bFriendData = await friendDao.isFriend(key);
        if (bFriendData) {
            await redis.set(cacheKey, 1, 60 * 60 * 24 * 3); // 缓存时间三天
            return true;
        }

        await redis.set(cacheKey, 0, 60 * 60 * 24 * 3); // 缓存时间三天
        return false;
    }

    async searchFriend(uid, phone) {
        let ret = await friendDao.searchFriend(uid, phone);
        if (!ret){
            throw new BusiError(500, "好友不存在");
        }

        return ret;
    }

    async agreeFriend(uid, friendUid) {
        let key = this.getKey(uid, friendUid);
        let ret = await friendDao.agreeApllyFriend(uid, friendUid);
        if (ret && ret.agree === 11) {
            let cacheKey = redisConst.USER_FRIEND_FLAG + ":" + key;
            await redis.set(cacheKey, 1, 60 * 60 * 24 * 3); // 缓存时间三天
        }
        return ret;
    }
    async applyBeFriend(uid, friendUid) {
        let myinfo = await userDao.getUserByIdAsync(uid);
        let friendInfo = await userDao.getUserByIdAsync(friendUid);
        if(!friendInfo){
            friendInfo = await userDao.getUserByMobile(friendUid);
        }
        
        if (!myinfo || !friendInfo) {
            throw new BusiError(500, "未找到对应玩家");
        }
        
        let ret = await friendDao.applyBeFriend(myinfo, friendInfo);
        if(ret){
            // if(ret.agree === 11){
            //     return;
            // }
            //
            // let agree = this.getAgree(Number(uid), Number(friendInfo.uid));
            // let res = await friendDao.updateAgree(ret.Id, agree);
            // if(res <= 0){
            //     throw new BusiError(500, "申请失败");
            // }

            let info = ret;
            if (info.agree === 1 && friendInfo.uid > myinfo.uid){
                let resin = await friendDao.agreeApllyFriend(myinfo.uid,friendInfo.uid);
                if(!resin){
                    throw new BusiError(500, "申请好友失败");
                }
            }else if(info.agree === 10 && friendInfo.uid < myinfo.uid){
                let resin = await friendDao.agreeApllyFriend(myinfo.uid,friendInfo.uid);
                if(!resin){
                    throw new BusiError(500, "申请好友失败");
                }
            }
            return;
        }

        let res = await friendDao.save(myinfo, friendInfo);
        if(res <= 0){
            throw new BusiError(500, "申请好友失败");
        }

        let key = this.getKey(myinfo.uid, friendInfo.uid);
        let cacheKey = redisConst.USER_FRIEND_FLAG + ":" + key;
        let bFriend = await redis.get(cacheKey);
        if (!bFriend){
            await redis.set(cacheKey, 1, 60 * 60 * 24 * 3); // 缓存时间三天
        }
    }

    async deleteFriend(uid, friendUid) {
        let ret = await friendDao.deleteFriend(uid, friendUid);
        if (ret <= 0) {
            throw new BusiError(500, "刪除好友失败");
        }

        let key = this.getKey(uid, friendUid);
        let cacheKey = redisConst.USER_FRIEND_FLAG + ":" + key;
        await redis.del(cacheKey);
    }

    getKey(uid, friendUid){
        let n1 = Number(uid); 
        let n2 = Number(friendUid);
        let key = n1 + '_' + n2;
        if (n1 > n2) key = n2 + '_' + n1;

        return key;
    }

    getAgree(myUID, otherUID){
        let n1 = myUID;
        let n2 = otherUID;

        let agree = 1;
        if (n2 < n1) {
            agree = 10;
        }

        return agree;
    }
}


let service = new FriendService();
module.exports = service;
